<?php

namespace ipenge\webuploader\helpers;

use Yii;

class StringHelper
{
    public static function getLocalFilePath($url, $type = 'images')
    {
        $prefix =  Yii::getAlias("@root/") . 'web';
        if (Yii::$app->params['uploadConfig'][$type]['fullPath'] == true)
        {
            $url = str_replace(Yii::$app->request->hostInfo, '', $url);
        }

        return $prefix . $url;
    }

    /**
     * 获取数字随机字符串
     *
     * @param bool $prefix 判断是否需求前缀
     * @param int $length 长度
     * @return string
     */
    public static function randomNum($prefix = false, $length = 8)
    {
        $str = $prefix ?? '';
        return $str . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, $length);
    }

    /**
     * 创建缩略图地址
     *
     * @param string $url
     * @param int $width
     * @param int $height
     */
    public static function createThumbUrl($url, $width, $height)
    {
        $url = explode('/', $url);
        $nameArr = explode('.', end($url));
        $url[count($url) - 1] = $nameArr[0] . "@{$width}x{$height}." . $nameArr[1];

        return implode('/', $url);
    }
}